<template>
	<view class="container">
		<view class="header">
			<view class="avatar">
				<image :src="userInfo.header" />
			</view>
			<view class="username">{{userInfo.nickname}}</view>
		</view>
		<view class="content">
			<view class="container-title">
				<view class="input-title">标题：</view>
				<input class="input-content" type="text" placeholder="请输入标题" v-model="title"></input>
			</view>
			<textarea v-model="content" placeholder="分享你的美好生活" maxlength="200" show-confirm-bar auto-height></textarea>
			<view class="images">
				<view class="image" v-for="(image, index) in images" :key="index">
					<image :src="image" />
					<view class="delete" @click="deleteImage(index)">×</view>
				</view>
				<view class="add" v-show="images.length < 9" @click="chooseImage">+</view>
			</view>
		</view>
		<view class="tags">
			<mentionable-input @mention="handleMention" @handle-subject="handleSubject"></mentionable-input>
		</view>
		<view class="home">
			<!-- 显示当前位置信息 -->
			<location @handle-event="handlePostion"></location>
		</view>
		<view class="footer">
			<button size="mini" class="cancel" @click="cancel">存草稿</button>
			<button size="mini" class="publish" @click="publish">发布</button>
		</view>
	</view>
</template>
<script>
	import {
		OSS_URL
	} from '@/commons/constants/CommonURL.js'

	import {
		artPublish
	} from '@/api/art.js'

	import {
		getOSSPolicy
	} from '@/api/third.js'

	import StringUtils from '@/commons/utils/StringUtils.js'

	import Location from '@/componts/common/Position/Location.vue';
	import MentionableInput from '@/componts/common/MentionableInput/MentionableInput.vue'
	export default {
		data() {
			return {
				content: "",
				/**
				 * 图片信息
				 */
				images: [],
				/**
				 * 当前用户信息
				 */
				userInfo: {

				},
				/**
				 * 标题
				 */
				title: '',
				/**
				 * 定位
				 */
				position: {
					lng: '116.401445',
					lat: '39.922806',
				},
				subjectList: [],
				/**
				 * 认证凭证
				 */
				policy: {
					policy: '',
					signature: '',
					OSSAccessKeyId: '',
					success_action_status: '200'
				}
			};
		},
		components: {
			MentionableInput,
			Location
		},
		/**
		 * 页面渲染的时候
		 */
		mounted() {
			this.getPolicy()
			this.getUserInfo()
		},
		methods: {
			/**
			 * 获取标题传参
			 * @param {Object} data
			 */
			handleSubject(data) {
				this.subjectList.push(data)
			},
			/**
			 * 获取子组件的值
			 * @param {Object} data
			 */
			handlePostion(data) {
				this.position = data
			},
			handleMention(username) {
				console.log(`@${username}`)
			},
			getUserInfo() {
				this.userInfo = JSON.parse(uni.getStorageSync("userInfo"));
			},
			/**
			 * 获取文件上传凭证
			 */
			async getPolicy() {
				let response = await getOSSPolicy()
				this.policy.policy = response.policy
				this.policy.signature = response.signature
				this.policy.OSSAccessKeyId = response.accessid
			},
			/**
			 * OSS上传图片
			 */
			chooseImage() {
				uni.chooseImage({
					count: 9 - this.images.length,
					success: (res) => {
						res.tempFiles.forEach((item, index) => {
							let tiemr = new Date();
							// 封装文件前缀时间
							let address = tiemr.getFullYear() + '-' + (((tiemr.getMonth() + 1) >= 10) ?
									(tiemr.getMonth() + 1) : '0' + (tiemr.getMonth() + 1)) + '-' +
								tiemr.getDate();
							address = address + '/art/artphoto/';
							//获取到文件路径
							var imageSrc = res.tempFilePaths[index];
							// 获取文件后缀
							let str = StringUtils.getFileExtensionHasSign(item.name)
							let nameStr = address + tiemr.getTime() + str;
							let ossUrl = OSS_URL;
							//最终文件名
							let finalFileURL = ossUrl + '/' + nameStr;
							uni.uploadFile({
								url: ossUrl,
								filePath: imageSrc,
								fileType: 'image',
								name: 'file',
								formData: {
									name: nameStr,
									key: nameStr,
									...this.policy
								},
								success: res => {
									if (res.statusCode == '200') {
										// 如果上传成功则放入图片数组
										this.images.push(finalFileURL);
									} else {
										uni.showToast({
											title: '上传失败，请重试',
											icon: null
										})
									}
								}
							});
						})
					},
				});
			},
			deleteImage(index) {
				this.images.splice(index, 1);
			},
			cancel() {
				uni.navigateBack();
			},
			async publish() {
				// 在这里编写发布的逻辑
				// 可以使用uni.request发送发布请求
				let response = await artPublish({
					photoContext: this.content,
					files: this.images,
					position: this.position,
					titleTags: this.subjectList,
					photoTitle: this.title
				})

				if (response.code == 200) {
					// 发布成功的处理逻辑
					uni.showToast({
						title: "发布成功",
						icon: "success",
					});
					uni.navigateBack();
				} else {
					// 发布失败的处理逻辑
					uni.showToast({
						title: "发布失败，请稍后再试",
						icon: "none",
					});
				}
			},
		},
	};
</script>
<style>
	.container-title {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.input-title {
		width: 60px;
		font-size: 16px;
		line-height: 44px;
	}

	.input-content {
		height: 44px;
		flex: 1;
		padding: 10px;
		font-size: 16px;
		border: 1px solid #ccc;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 90vh;
	}

	.header {
		display: flex;
		align-items: center;
		padding: 20px;
	}

	.avatar {
		width: 40px;
		height: 40px;
		margin-right: 10px;
		border-radius: 50%;
		overflow: hidden;
	}

	.avatar image {
		width: 100%;
		height: 100%;
	}

	.username {
		font-size: 16px;
		color: #333;
	}

	.content {
		flex: .8;
		padding: 20px;
	}

	textarea {
		width: 95%;
		height: 100px;
		padding: 10px;
		background-color: #f5f5f5;
		border: none;
		border-radius: 5px;
		font-size: 16px;
		color: #333;
		margin-bottom: 20px;
	}

	.images {
		display: flex;
		flex-wrap: wrap;
	}

	.image {
		position: relative;
		width: 200rpx;
		height: 200rpx;
		margin-right: 10px;
		margin-bottom: 10px;
		overflow: hidden;
	}

	.image image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.delete {
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		line-height: 20px;
		text-align: center;
		background-color: rgba(0, 0, 0, 0.5);
		color: #fff;
		border-radius: 50%;
		cursor: pointer;
	}

	.add {
		position: relative;
		width: 200rpx;
		height: 200rpx;
		margin-right: 10px;
		margin-bottom: 10px;
		background-color: #f5f5f5;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 30px;
		color: #999;
		cursor: pointer;
	}

	.footer {
		display: flex;
		justify-content: space-between;
		padding: 20px;
	}

	.home {
		margin-top: 20px;
	}

	.cancel {
		width: 50px;
		height: 50px;
		padding: 10px;
		background-color: #a72cff;
		border: none;
		border-radius: 50%;
		font-size: 16px;
		color: #fff;
	}


	.publish {
		width: 76%;
		padding: 10px;
		background-color: #ff0223;
		border: none;
		border-radius: 30px;
		font-size: 16px;
		color: #fff;
	}
</style>